import React from 'react';
import {RouteItem} from "../../Pages/type";
import {isArray} from 'lodash';
import {Col, Menu} from "antd";

function item2Menu (rs : RouteItem,  authInfo:string[]){
  if(rs.hide){
    return null;
  }
  if(rs.key && rs.name && rs.component){
    if(!isArray(rs.children) ){
      if(!rs.needAuth || (rs.needAuth && authInfo.includes(rs.key))){
        return <Menu.Item key={rs.key} icon={rs.icon || null} >
          <span className={'global-menu-text'}>{rs.name}</span>
        </Menu.Item>
      }
      return null;
    }
    let menus = config2Menu(rs.children, authInfo);
    if(isArray(menus) && menus.length>0){
      return <Menu.SubMenu key={rs.key} title={rs.name} icon={rs.icon}>
        {menus}
      </Menu.SubMenu>
    }
    return null;
  }
  return null;
}
// 将 路由配置转换为 菜单
export function config2Menu(rs : RouteItem | RouteItem[], authInfo:string[]){
  if(!isArray(rs)){
    let item = item2Menu(rs,authInfo);
    if(item){
      return [item];
    }
    return [];
  }

  let ar= [];
  for(let i in rs){
    let item = rs[i];
    let mItem = item2Menu(item, authInfo);
    if(mItem){
      ar.push(mItem);
    }
  }
  return ar;
}
